package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/steel/api")
@AllArgsConstructor
public class SteelController {

    private final HttpServletRequest httpServletRequest;
    private final SteelService steelService;
    private final SteelDetailService steelDetailService;
    private final SteelSolventService steelSolventService;
    private final SteelCarbonSequestrationService steelCarbonSequestrationService;
    private final SteelRawMaterialService steelRawMaterialService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Steel steel) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getNcv())) {
                ncvs = Arrays.asList(steel.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getFc())) {
                fcs = Arrays.asList(steel.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getCc())) {
                ccs = Arrays.asList(steel.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getOf())) {
                ofs = Arrays.asList(steel.getOf().split(","));
            }
            List<String> pis = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getPis())) {
                pis = Arrays.asList(steel.getPis().split(","));
            }
            List<String> dxis = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getDxis())) {
                dxis = Arrays.asList(steel.getDxis().split(","));
            }
            List<String> efis = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getEfis())) {
                efis = Arrays.asList(steel.getEfis().split(","));
            }
            List<String> mis = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getMis())) {
                mis = Arrays.asList(steel.getMis().split(","));
            }
            List<String> efiPurchases = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getEfiPurchases())) {
                efiPurchases = Arrays.asList(steel.getEfiPurchases().split(","));
            }
            List<String> adCarbonSequestrations = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getAdCarbonSequestrations())) {
                adCarbonSequestrations = Arrays.asList(steel.getAdCarbonSequestrations().split(","));
            }
            List<String> efCarbonSequestrations = new ArrayList<>();
            if (StringUtils.isNotBlank(steel.getEfCarbonSequestration())) {
                efCarbonSequestrations = Arrays.asList(steel.getEfCarbonSequestration().split(","));
            }

            Double eBurn = SteelUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eProcess = SteelUtil.getEProcess(pis, dxis, efis, steel.getPElectrode(), steel.getEfElectrode(), mis, efiPurchases);
            Double ePurchaseElectric = SteelUtil.getEPurchaseElectric(steel.getAdPurchaseElectric(), steel.getEfPurchaseElectric());
            Double ePurchaseHeat = SteelUtil.getEPurchaseHeatOrEOutputHeat(steel.getAdPurchaseHeat());
            Double eOutputHeat = SteelUtil.getEPurchaseHeatOrEOutputHeat(steel.getAdOutInputHeat());
            Double rCarbonSequestration = SteelUtil.getRCarbonSequestration(adCarbonSequestrations, efCarbonSequestrations);
            Double eOutputElectric = SteelUtil.getEOutputElectric(steel.getAdOutInputElectric(), steel.getEfOutInputElectric());

            Double result = SteelUtil.getE(eBurn, eProcess, ePurchaseElectric,
                    ePurchaseHeat, rCarbonSequestration, eOutputElectric, eOutputHeat);

            steel.setCustomerId(customer.getId());
            steel.setCreateTime(new Date());
            steel.setEBurn(GasDCUtil.round(eBurn, 2));
            steel.setEProcess(GasDCUtil.round(eProcess, 2));
            steel.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            steel.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            steel.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            steel.setRCarbonSequestration(GasDCUtil.round(rCarbonSequestration, 2));
            steel.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            steel.setState(Constants.STATE_INVAILD);
            steel.setResult(GasDCUtil.getThousandth(result));
            steelService.save(steel);
            for (int i = 0; i < ncvs.size(); i++) {
                SteelDetail steelDetail = new SteelDetail();
                steelDetail.setNcv(Double.valueOf(ncvs.get(i)));
                steelDetail.setFc(Double.valueOf(fcs.get(i)));
                steelDetail.setCc(Double.valueOf(ccs.get(i)));
                steelDetail.setOf(Double.valueOf(ofs.get(i)));
                steelDetail.setSteelId(steel.getId());
                steelDetailService.save(steelDetail);
            }
            for (int i = 0; i < mis.size(); i++) {
                SteelRawMaterial steelRawMaterial = new SteelRawMaterial();
                steelRawMaterial.setMi(Double.valueOf(mis.get(i)));
                steelRawMaterial.setEfi(Double.valueOf(efis.get(i)));
                steelRawMaterial.setSteelId(steel.getId());
                steelRawMaterialService.save(steelRawMaterial);
            }
            for (int i = 0; i < pis.size(); i++) {
                SteelSolvent steelSolvent = new SteelSolvent();
                steelSolvent.setEfi(Double.valueOf(efis.get(i)));
                steelSolvent.setDxi(Double.valueOf(dxis.get(i)));
                steelSolvent.setPi(Double.valueOf(pis.get(i)));
                steelSolvent.setSteelId(steel.getId());
                steelSolventService.save(steelSolvent);
            }
            for (int i = 0; i < adCarbonSequestrations.size(); i++) {
                SteelCarbonSequestration steelCarbonSequestration = new SteelCarbonSequestration();
                steelCarbonSequestration.setAdCarbonSequestration(Double.valueOf(adCarbonSequestrations.get(i)));
                steelCarbonSequestration.setEfCarbonSequestration(Double.valueOf(efCarbonSequestrations.get(i)));
                steelCarbonSequestration.setSteelId(steel.getId());
                steelCarbonSequestrationService.save(steelCarbonSequestration);
            }
            return ResultUtil.success("计算成功", steel);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
